let express = require('express');
let url = require('url');
let swig = require('swig');
let querystring = require("querystring");
let httpRequest = require('request');
let baseUrl = 'http://47.102.99.70:8080/ruoyi-admin'
// let baseUrl = 'http://192.168.10.86:7890'
let path = require('path');
app = express();
//静态文件目录
app.use(express.static(path.join(__dirname, './public')));
//设置渲染文件的目录
app.set('views','./views');
//设置html模板渲染引擎
app.engine('html',swig.renderFile);
//设置渲染引擎为html
app.set('view engine','html');
app.use(express.static( '/public'));
app.listen(8005);
console.log('http://127.0.0.1:8005');
console.log('node服务启动成功。')

//调用路由，进行页面渲染
// 首页
app.get('/',function(request,response){
    //调用渲染模板
    response.render('index.html',{
        //传参
        title:'首页', content:'首页 template'
    });
});
//pie
app.get('/pie',function(request,response){
    //调用渲染模板
    response.render('pie.html',{
        //传参
        title:'饼状图', content:'te'
    });
});
app.get('/pre',function(request,response){
    //调用渲染模板
    response.render('pre.html',{
        //传参
        title:'预测页面', content:'te'
    });
});
app.get('/hurt',function(request,response){
    //调用渲染模板
    response.render('hurt.html',{
        //传参
        title:'小麦病虫害', content:'dd'
    });
});
app.get('/mostLike',function(request,response){
    //调用渲染模板
    response.render('mostLike.html',{
        //传参
        title:'最受欢迎农产品价格比较', content:'dd'
    });
});
app.get('/earth',function(request,response){
    //调用渲染模板
    response.render('earth.html',{
        //传参
        title:'百度地图', content:'dd'
    });
});
app.get('/line',function(request,response){
    //调用渲染模板
    response.render('line.html',{
        //传参
        title:'农业生产总值预测', content:'dd'
    });
});
app.get('/line_product',function(request,response){
    //调用渲染模板
    response.render('line_product.html',{
        //传参
        title:'农业生产总值预测', content:'dd'
    });
});
app.get('/mostLike_addr',function(request,response){
    //调用渲染模板
    response.render('mostLike_addr.html',{
        //传参
        title:'农业生产总值预测', content:'dd'
    });
});

//get请求获取参数 用query
app.get('/api', function (request, response) {
    // 解析请求，包括文件名
    var pathname = url.parse(request.url).pathname;
    // 输出请求的文件名
    console.log("Request for " + pathname + " received.");
    console.log(request.method);//get
    console.log(request.query);//{ a: '3', b: '4', c: '5' }
    console.log(request.query.method); //3
    response.send("hello");
})

//post AJax请求获取参数 用body
app.post('/api', function (request, res) {
    // 解析请求，包括文件名
    var pathname = url.parse(request.url).pathname;
    console.log("nodejs.request.method = "+pathname)
    let getData = "";
    request.on("data", (data) => {
        getData += data
    })
    request.on("end",() => {
        const data = querystring.parse(getData);
        // console.log(getData);
        // console.log(data); //  { age: '8', height: '185' }
        // console.log(data.method) //  { age: '8', method: 'http://xx' }
        var url  = baseUrl + data.method;
        console.log(url);
        // console.log(data);
        httpRequest({
            url: url,
            method: "POST",
            json: true,
            headers: {
                "content-type": "application/json",
            },
            body: data
        }, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                // console.log(body) // 请求成功的处理逻辑
                res.send(body);
            }
        });
    })

})


